package com.unfind.myahmt.xyb.network

import android.content.Context
import com.google.gson.Gson
import com.unfind.myahmt.sports.network.ApiErrorModel
import com.unfind.myahmt.xyb.util.ToastUtil
import com.unfind.myahmt.xyb.view.KTLoadingDialog
import com.xb.sourest.AndroidApplication
import com.xb.sourest.api.NetError
import com.xb.sourest.base.IModel
import io.reactivex.subscribers.ResourceSubscriber
import retrofit2.HttpException
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException

/**
 * 作用：请求的返回Response
 * 作者：unfind
 * 时间：2019年05月09日17:29:25
 */
abstract class ApiResponse<T : IModel>(private val context: Context) : ResourceSubscriber<T>() {


    abstract fun success(data: T)
    abstract fun reLogin()  //登陆失效
    abstract fun failure(statusCode: Int, apiErrorModel: ApiErrorModel)

    private var isShowLoading: Boolean = true
    constructor(context: Context, isShowLoading: Boolean) : this(context) {
        this.isShowLoading = isShowLoading
    }
    override fun onStart() {
        super.onStart()
        if (isShowLoading) {
            KTLoadingDialog.show(context)
        }
    }

    override fun onNext(t: T) {
        success(t)
    }

    override fun onComplete() {
        KTLoadingDialog.cancel()
    }

    override fun onError(e: Throwable) {
        e.printStackTrace()
        var error: NetError?=null
        KTLoadingDialog.cancel()
        if (null != e){
            if (e !is NetError){
                if (e is HttpException) {
                    val apiErrorModel: ApiErrorModel = when (e.code()) {
                        ApiErrorType.INTERNAL_SERVER_ERROR.code ->
                            ApiErrorType.INTERNAL_SERVER_ERROR.getApiErrorModel(context)
                        ApiErrorType.BAD_GATEWAY.code ->
                            ApiErrorType.BAD_GATEWAY.getApiErrorModel(context)
                        ApiErrorType.NOT_FOUND.code ->
                            ApiErrorType.NOT_FOUND.getApiErrorModel(context)
                        else -> otherError(e)
                    }
                    failure(e.code(), apiErrorModel)
                    return
                }
                val apiErrorType: ApiErrorType = when (e) {
                    is UnknownHostException -> ApiErrorType.NETWORK_NOT_CONNECT
                    is ConnectException -> ApiErrorType.NETWORK_NOT_CONNECT
                    is SocketTimeoutException -> ApiErrorType.CONNECTION_TIMEOUT
                    else -> ApiErrorType.UNEXPECTED_ERROR
                }
                failure(apiErrorType.code, apiErrorType.getApiErrorModel(context))
                return
            }else {
                error = e
            }
            if (error.type == 6){
                reLogin()
            }else{
                onFail(error)
            }
        }

    }
    protected fun onFail(error: NetError) {
        ToastUtil.show(AndroidApplication.getApplication(),error.message!!)
    }

    private fun otherError(e: HttpException) =
            Gson().fromJson(e.response().errorBody()?.charStream(), ApiErrorModel::class.java)
}